(function($){
	var count_date = function(date, day){
		if(date != '' && day != ''){
			var datetime = new Date(date);
			var result = new Date(date);
			result.setDate(datetime.getDate() + parseInt(day));
			var year = result.getFullYear();
			var month = result.getMonth() + 1 < 10 ? '0' + (result.getMonth() + 1) : result.getMonth() + 1;
			var day = result.getDate() < 10 ? '0' + result.getDate() : result.getDate();
			return year + '-' + month + '-' + day;
		}else{
			return "";
		}
	};
	
	var get_version = function(){
		$.getJSON("/get_version/", {'id': $("#id_app").val()}, function(data){
    		var html = "<option value=''>---------</option>";
    		$.each(data.res, function(k, v){
    			if(v == $("#id_version").val()){
    				html += "<option selected='selected' value='" + v + "'>" + v + "</option>";
    			}else{
	    			html += "<option value='" + v + "'>" + v + "</option>";
    			}
    		});
    		$("#select_version").html(html);
    	});
	}
	
	var get_rating = function(){
		$.getJSON("/get_rating/", {'id': $("#id_app").val()}, function(data){
    		var html = "<option value=''>---------</option>";
    		$.each(data.res, function(k, v){
    			if(v == $("#id_rating").val().split(", ")[0]){
    				html = "<option selected='selected' value='" + v + "'>" + v + "</option>";
    			}else{
	    			html += "<option value='" + v + "'>" + v + "</option>";
    			}
    		});
    		$("#select_rating").html(html);
    		if($("#id_rating").val() != null){
	    		$("#text_rating").val($("#id_rating").val().split(", ")[1]);
    		}
    	});
	}
	
	$(document).ready(function(){
		
		$("#id_version").css('display', 'none');
		$("#id_version").after("\
	<select id='select_version' name='version'>\
		<option value=''>---------</option>\
	</select>\
		");
		
		get_version();
		get_rating();
		
		if($("div.field-get_territories ").length != 0){
			$("div.field-prefix ").css('display', 'none');
		}
		
		$("div.field-territories ").css('display', 'none');
		$("#id_app").css('display', 'none');
		$("#lookup_id_app").after("&nbsp;&nbsp;&nbsp;<a target='_blank' href='##' id='id_app_name'></a>");
		$("#id_app_name").parent().append("<span class='help-block'><strong><i>Click the 'magnifying glass' icon to choose an app</i></strong></span>");
		
		$("#id_rej").css('display', 'none');
		$("#lookup_id_rej").after("&nbsp;&nbsp;&nbsp;<a target='_blank' href='##' id='id_rej_id'></a>");
		$("#id_rej_id").parent().append("<span class='help-block'><strong><i>Click the 'magnifying glass' icon to choose a rejection</i></strong></span>");
		
		$("#id_rating").css('display', 'none');
		$("#id_rating").after("\
	<select id='select_rating' name='select_rating' style='width: 130px;'>\
		<option value=''>---------</option>\
	</select>\
	<input type='text' style='width: 200px;' id='text_rating' name='text_rating' />\
		");
		
		$("#id_prefix").after("\
	<textarea class='autosize' name='countries' id='countries' \
		style='display:none; overflow: hidden; word-wrap: break-word; resize: none; width: 200px; height: 18px;'></textarea>\
	<script type='text/javascript'>Suit.$('#countries').autosize();</script>\
		");
		
		if($("#id_prefix option[value='A']").attr('selected') != true){
			$("#countries").css('display', 'inline-block');
			$("#countries").val($("div.field-territories div div.controls span").html());
		}
		
		$("#id_prefix").change(function(e){
			if(e.target.value != 'A'){
				$("#countries").css('display', 'inline-block');
				$("#countries").val($("div.field-territories div div.controls span").html());
			}else{
				$("#countries").css('display', 'none');
				$("#countries").val("");
			}
		});
		
		$("div.field-update_assets ").css('display', 'none');
		$("div.field-update_meta ").css('display', 'none');
		$("div.field-rej ").css('display', 'none');
		$("div.field-get_rej ").css('display', 'none');
		$("div.field-get_update_assets ").css('display', 'none');
		$("div.field-get_update_meta ").css('display', 'none');

		// heyulin 2016-3-17 ,because of need hide 'div.field-update_meta',--see comment[1]
		// but i haven't found '#id_type' in anywhere . '#id_type' may not exist
		if($("#id_type option[value='U']").attr('selected') == true){
			$("div.field-update_for ").css('display', 'block');
			$("div.field-update_assets ").css('display', 'block');
			//$("div.field-update_meta ").css('display', 'block');   // [1]
		}
		
		if($("#id_is_resub option[value='Y']").attr('selected') == true){
			$("div.field-rej ").css('display', 'block');
			// $("div.field-resub_req ").css('display', 'block');
		}
		
		if($("font[data-id='UBF']").length != 0){
			$("#qa_request").css('display', 'block');
		}else{
			$("#qa_request").css('display', 'none');
		}
		if($("font[data-id='UMDAMK']").length != 0){
			$("#marketing_ops").css('display', 'block');
		}else{
			$("#marketing_ops").css('display', 'none');
		}
		
		if($("input[type='checkbox'][name='resub_req'][value='UMDAMK']").attr('checked') == true){
			$("#marketing_ops").css('display', 'block');
			$("label[for='id_prefix']").attr('class', 'required');
		}
		if($("input[type='checkbox'][name='resub_req'][value='UBF']").attr('checked') == true){
			$("#qa_request").css('display', 'block');
			$("label[for='id_edqa']").attr('class', 'required');
			$("label[for='id_etqa']").attr('class', 'required');
		}
		
		if($("div.field-get_type div div.controls span").html() == 'Update'){
			$("div.field-get_update_assets ").css('display', 'block');
			$("div.field-get_update_meta ").css('display', 'block');			
		}
		
		if($("div.field-is_resub div div.controls span").html() == 'Yes'){
			$("div.field-get_rej ").css('display', 'block');
		}
		
		if($("div.field-get_app_store div div.controls span").html () != 'iTunes App Store'){
			$("div.field-get_apple_id ").css('display', 'none');
			$("div.field-get_special_settings ").css('display', 'none');
		}else{
			$("div.field-get_apple_id ").css('display', 'block');
			$("div.field-get_special_settings ").css('display', 'block');
		}
		
		// $("#id_type").change(function(e){
			// if(e.target.value == 'N'){
				// $("#id_version").val('1.0');
				// $("div.field-update_assets ").css('display', 'none');
				// $("div.field-update_meta ").css('display', 'none');
				// $("div.field-update_for ").css('display', 'none');
			// }else{
				// $("#id_version").val('');
				// $("div.field-update_assets ").css('display', 'block');
				// $("div.field-update_meta ").css('display', 'block');
				// $("div.field-update_for ").css('display', 'block');
			// }
		// });
		
		$("#id_is_resub").change(function(e){
			if(e.target.value == 'Y'){
				$("div.field-rej ").css('display', 'block');
				// $("div.field-resub_req ").css('display', 'block');
			}else{
				$("div.field-rej ").css('display', 'none');
				// $("div.field-resub_req ").css('display', 'none');
			}
		})
		
		$("input[type='checkbox'][name='resub_req']").change(function(e){
			if(e.target.value == 'UBF'){
				if($(this).attr('checked') == true){
					$("#qa_request").css('display', 'block');
					$("label[for='id_edqa']").attr('class', 'required');
					$("label[for='id_etqa']").attr('class', 'required');
				}else{
					$("#qa_request").css('display', 'none');
				}
			}else if(e.target.value == 'UMDAMK'){
				if($(this).attr('checked') == true){
					$("#marketing_ops").css('display', 'block');
					$("label[for='id_prefix']").attr('class', 'required');
				}else{
					$("#marketing_ops").css('display', 'none');
				}
			}
		});
		
		$("#id_edqa").blur(function(){
			$("#id_edsr").val(count_date($(this).val(), $("#id_etqa").val()));
		});
		$("#id_etqa").blur(function(){
			$("#id_edsr").val(count_date($("#id_edqa").val(), $(this).val()));
		});


		// [1] heyulin- 2016-03-15 edit ,because of echo need not show "Update on Meta Data:"
		
		$("#select_version").change(function(e){
			if(e.target.value == '1.0'){
				$("input[type='checkbox'][name='resub_req']").attr('checked', 'checked');
				$("#qa_request").css('display', 'block');
				$("#marketing_ops").css('display', 'block');
				$("div.field-update_assets ").css('display', 'none');
				//$("div.field-update_meta ").css('display', 'none');     // [1]
			}else{
				$("input[type='checkbox'][name='resub_req']").removeAttr('checked');
				$("#qa_request").css('display', 'none');
				$("#marketing_ops").css('display', 'none');
				$("div.field-update_assets ").css('display', 'block');
				//$("div.field-update_meta ").css('display', 'block');    // [1]
				
			}
			// $.getJSON('/get_type/', {'id': $("#id_app_name").attr('data-id'), 'version': e.target.value}, function(data){
				// console.log(data.res);
			// })
		});
		
		// [2] heyulin- 2016-03-15 edit ,because of echo need not show "Suggestion on App Rating:"
		$('div.control-group.form-row.field-rating').hide();   // [2]
		
		$("div.field-valid_date ").css('display', 'none');
		$("div.field-binary_ready_date ").css('display', 'none');
		$("div.field-meta_ready_date ").css('display', 'none');
		$("div.field-binary_uploaded_date ").css('display', 'none');
//		$("div.field-app_submit_date ").css('display', 'none');
//		$("div.field-sub_person ").css('display', 'none');
		
		if($("#id_validation").val() == 'V'){
			$("div.field-valid_date ").css('display', 'block');
		}
		if($("#id_binary_status").val() == 'Y'){
			$("div.field-binary_ready_date ").css('display', 'block');
		}
		if($("#id_meta_status").val() == 'Y'){
			$("div.field-meta_ready_date ").css('display', 'block');
		}
		if($("#id_binary_upload_status").val() == 'Y'){
			$("div.field-binary_uploaded_date ").css('display', 'block');
		}
		if($("#id_status").val() == 'S'){
			$("div.field-app_submit_date ").css('display', 'block');
			$("div.field-sub_person ").css('display', 'block');
		}
		
		$("#id_validation").change(function(e){
			if(e.target.value == 'V'){
				$("div.field-valid_date ").css('display', 'block');
			}else{
				$("div.field-valid_date ").css('display', 'none');
			}
		});
		$("#id_binary_status").change(function(e){
			if(e.target.value == 'Y'){
				$("div.field-binary_ready_date ").css('display', 'block');
			}else{
				$("div.field-binary_ready_date ").css('display', 'none');
			}
		});
		$("#id_meta_status").change(function(e){
			if(e.target.value == 'Y'){
				$("div.field-meta_ready_date ").css('display', 'block');
			}else{
				$("div.field-meta_ready_date ").css('display', 'none');
			}
		});
		$("#id_binary_upload_status").change(function(e){
			if(e.target.value == 'Y'){
				$("div.field-binary_uploaded_date ").css('display', 'block');
			}else{
				$("div.field-binary_uploaded_date ").css('display', 'none');
			}
		});
		$("#id_status").change(function(e){
			if(e.target.value == 'S'){
				$("div.field-app_submit_date ").css('display', 'block');
				$("div.field-sub_person ").css('display', 'block');
			}else{
				$("div.field-app_submit_date ").css('display', 'none');
				$("div.field-sub_person ").css('display', 'none');
			}
		})
		
		$("#id_price").css('width', '200px').css('display', 'none');
		$("#id_price").before("\
	<select id='select_price' style='width: 130px;'>\
		<option value='free'>Free</option>\
		<option value='paid'>Paid</option>\
	</select>\
		");
		
		if($("#id_price").val() != '0.0'){
			$("#select_price").val('paid');
			$("#id_price").css('display', 'inline-block');
		}else{
			$("#select_price").val('free');
			$("#id_price").css('display', 'none');
		}
		
		$("#select_price").change(function(e){
			if(e.target.value == 'paid'){
				$("#id_price").css('display', 'inline-block');
			}else{
				$("#id_price").css('display', 'none');
				$("#id_price").val('0.0');
			}
		});
		
		var rating = $("#id_rating").val();
		if(rating != null){
			var rating_selected = rating.split(", ")[0];
			var rating_text = rating.split(", ")[1];
		}
		
	});
})(django.jQuery);
